package com.ochnas.volunteersignup.ui;

import java.util.Calendar;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import com.ochnas.volunteersignup.domain.Time;
import static com.ochnas.volunteersignup.ui.SharedConstants.*;
public class TimeConverter implements Converter {
	private Calendar calendar = Calendar.getInstance();
	
	/**
	 *  <h:outputText value="#{myBean.timeProperty}">
     		<f:converter converterId="com.ochnas.volunteersignup.ui.TimeConverter"/>
  		</h:outputText>
	 */
	
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
			throws ConverterException {
//		if (arg2 == null || "".equals(arg2)) return new Time();
		
		if(arg2 == null || !SIMPLE_TIME_PATTERN.matcher(arg2).matches()) {
	    	 FacesMessage message = new FacesMessage("Conversion error occurred. ", 
	    			 "Invalid time entered. Please follow example. ");
	    		     message.setSeverity(FacesMessage.SEVERITY_ERROR);
	    		     throw new ConverterException(message);
	     }
	     
	     int colonIndex = arg2.indexOf(":");
	     String hourStr = arg2.substring(0, colonIndex).trim();
	     String[] strArray = arg2.substring(colonIndex + 1).split("\\s");
	     int hourValue = 0;
	     int minutesValue = 0;
	     String timeOfDay;
	     boolean isValid = true;
	     try {
			hourValue = Integer.parseInt(hourStr);
			minutesValue = Integer.parseInt(strArray[0].trim());
			timeOfDay = strArray[1].trim();
			if (hourValue < 0 || hourValue > 12) isValid = false;
			if (minutesValue < 0 || minutesValue > 59) isValid = false;
			if (isValid) {
				// According to java.util.Calendar, midnight and noon are represented as 0
				// Range is 0 - 11
				if (hourValue == 12) hourValue = 0; 
				calendar.set(Calendar.HOUR, hourValue);
				calendar.set(Calendar.AM_PM, 
						(AM.equalsIgnoreCase(timeOfDay)? Calendar.AM : Calendar.PM));
			}
		} catch (NumberFormatException e) {
			isValid = false;
		}
		
		if (!isValid) {
		   	 FacesMessage message = new FacesMessage("Conversion error occurred. ", 
		   			"Invalid time entered. Please follow example. ");
		     message.setSeverity(FacesMessage.SEVERITY_ERROR);
		     throw new ConverterException(message);
		}
	     return new Time(calendar.get(Calendar.HOUR_OF_DAY), minutesValue);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ConverterException {		
		if (arg2 == null) return "";
		return arg2.toString();
	}
	
	public static void main(String[] args) {
		String v1 = "1";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());
		v1 = "1:00";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());
		v1 = "1:00 ";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());
		v1 = "1:00 pma";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());		
		v1 = "1:00 pm";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());	
		v1 = "1:00 PM";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());	
		v1 = "1:00 pM";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());	
		v1 = "01:00 pm";
		System.out.println(v1 + "? " + SIMPLE_TIME_PATTERN.matcher(v1).matches());		
	}
}
